package com.global.college.ui.fragment.lesson;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.global.college.MainActivity;
import com.global.college.R;
import com.global.college.adapter.FreeCourseAdapter;
import com.global.college.adapter.StudyCourseAdapter;
import com.global.college.api.ProgressSubscriber;
import com.global.college.api.RequestClient;
import com.global.college.base.BaseFragment;
import com.global.college.base.baselist.HnRecyclerGridDecoration;
import com.global.college.base.baselist.HsScrollViewPagerAdapter;
import com.global.college.constants.Constant;
import com.global.college.data.net.HomeGoodListBean;
import com.global.college.data.net.MyCourseListBean;
import com.global.college.ui.activity.BigImageActivity;
import com.global.college.ui.activity.InviteActivity;
import com.global.college.ui.activity.LoginByPswActivity;
import com.global.college.ui.activity.MyCourseActivity;
import com.global.college.ui.activity.OrderCenterActivity;
import com.global.college.ui.activity.SettingActivity;
import com.global.college.ui.activity.TrainingCampActivity;
import com.global.college.ui.activity.UserCenterActivity;
import com.global.college.ui.fragment.course.MyCourseXFragment;
import com.global.college.ui.fragment.course.MyCourseXFragmentKong;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.widget.tab.SlidingTabLayout;
import com.global.college.widget.tab.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author by xuyangsheng,  Date on 2024/7/16.
 * Explain：
 */
public class HomeCourseXFragment extends BaseFragment {

    @BindView(R.id.mSlidingTab)
    SlidingTabLayout mSlidingTab;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.recycler_free)
    RecyclerView recyclerFree;
    @BindView(R.id.txt_courses_have)
    TextView txt_courses_have;
    @BindView(R.id.txt_courses_finish)
    TextView txt_courses_finish;
    @BindView(R.id.layou_empty)
    LinearLayout layou_empty;
    @BindView(R.id.recycler_coursep)
    RecyclerView recyclerCoursep;
    private List<Fragment> mFragments = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    private int currentPosition = 0;

    private int Coursetype = 1;

    private int page = Constant.page;
    private int pageSize = 100;

    private FreeCourseAdapter freeCourseAdapter;

    //学习课程适配器
    private StudyCourseAdapter studyCourseAdapter;
    private List<MyCourseListBean.MyCourseBean> mData = new ArrayList<>();

    @Override
    public int getContentViewId() {
        return R.layout.fragment_lesson_x;
    }

    @Override
    public void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e("HomeCourseXFragment","initView");

        recyclerCoursep.setLayoutManager(new LinearLayoutManager(mActivity));
        studyCourseAdapter = new StudyCourseAdapter();
        studyCourseAdapter.bindToRecyclerView(recyclerCoursep);

    }

    @Override
    protected void initData() {
        Log.e("HomeCourseXFragment","initData");
        initTab();
        setListener();
        getCourseDataTotal(1,1);
        getCourseDataTotal(2,1);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.e("HomeCourseXFragment","父-setUserVisibleHint-isVisibleToUser:"+isVisibleToUser);
        if(isVisibleToUser){
            getCourseData(Coursetype,page);
        }
    }

    private void initTab() {
        List<String> strs = new ArrayList<>();
        mTitles.add(getString(R.string.order_studying));
        mTitles.add(getString(R.string.order_studyed));
        for (int i = 0; i < mTitles.size(); i++) {
            strs.add(mTitles.get(i));
            switch (mTitles.get(i)) {
                default:
                    mFragments.add(MyCourseXFragmentKong.newInstance(String.valueOf(i+1)));
                    break;
            }
        }
        HsScrollViewPagerAdapter adapter = new HsScrollViewPagerAdapter(mActivity.getSupportFragmentManager(), mFragments, strs);
        Log.e("HomeCourseXFragment","mFragments.size():"+mFragments.size());
        if (mFragments.size() < 1) {
            return;
        }
        viewPager.setAdapter(adapter);
        mSlidingTab.setViewPager(viewPager);
        viewPager.setCurrentItem(currentPosition);
    }

    private void setListener() {
        mSlidingTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                Log.e("HomeCourseXFragment","onTabSelect:"+position);
                viewPager.setCurrentItem(position);
                currentPosition = position;
                Coursetype = position + 1;
                page = 1;
                getCourseData(Coursetype,page);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void getCourseData(int coursetype,int pageNum) {
        RequestClient.getInstance().getCourseList( pageNum,  pageSize, coursetype).subscribe(new ProgressSubscriber<MyCourseListBean>(mActivity,false) {
            @Override
            public void onNext(MyCourseListBean model) {
                if (null != model){
                    if (page == 1) {
                        mData.clear();
                        if (null != model.getList() && model.getList().size() > 0) {
                            layou_empty.setVisibility(View.GONE);
                            mData.addAll(model.getList());
                        } else { //暂时无数据
                            getFreeCourse();
                        }
                    } else {
                        if (null != model.getList() && model.getList().size() > 0){
                            layou_empty.setVisibility(View.GONE);
                            mData.addAll(model.getList());
                        }
                    }
                    Log.e("studyCourseAdapter","mData长度:"+mData.size());
                    studyCourseAdapter.replaceData(mData);
                }else{ //暂时无数据
                    getFreeCourse();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);

            }
        });
    }


    private void getCourseDataTotal(int coursetype,int pageNum) {
        RequestClient.getInstance().getCourseList( pageNum,  pageSize, coursetype).subscribe(new ProgressSubscriber<MyCourseListBean>(mActivity,false) {
            @Override
            public void onNext(MyCourseListBean model) {
                if(coursetype==1){
                    if (null != model){
                        txt_courses_have.setText(model.getTotal()+"");
                    }else{ //暂时无数据
                        txt_courses_have.setText("0");
                    }
                }else if(coursetype==2){
                    if (null != model){
                        txt_courses_finish.setText(model.getTotal()+"");
                    }else{ //暂时无数据
                        txt_courses_finish.setText("0");
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);

            }
        });
    }

    public void getFreeCourse() {
        layou_empty.setVisibility(View.VISIBLE);
        RequestClient.getInstance().getGoodListBean(1).subscribe(new ProgressSubscriber<HomeGoodListBean>(mActivity, false) {
            @Override
            public void onNext(HomeGoodListBean bean) {
                if (freeCourseAdapter == null) {
                    freeCourseAdapter = new FreeCourseAdapter();
                    if (bean.getGoodList().size() == 0) {
                        return;
                    }
                    //设置为每行两列的列表
                    recyclerFree.setLayoutManager(new GridLayoutManager(mActivity, 2));
                    recyclerFree.addItemDecoration(new HnRecyclerGridDecoration(12));
                    freeCourseAdapter.bindToRecyclerView(recyclerFree);
                }
                freeCourseAdapter.replaceData(bean.getGoodList());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    @OnClick({R.id.rela_add_course_top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rela_add_course_top:
                ((MainActivity) mActivity).jump1Home();
                break;
        }
    }


}
